<?php
/**
 * 登录管理
 */
namespace app\api\controller;
use app\BaseController;
use think\facade\Db;
class Login extends BaseController
{
    public function initialize()
    {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Headers: X-Token,Content-Type, Content-Length, Authorization, Accept, X-Requested-With, Current-Page');
        header('Access-Control-Allow-Methods: POST,GET,OPTIONS,DELETE,PUT');
        parent::initialize();
    }

    /**
     * 用户登录验证
     * @return \josn
     */
    public function signIn()
    {
        try{
            // 数据接收
            $user_number = input("param.account");
            $user_pass = input("param.password");
            $captchaId = input("param.captchaId");
            $captchaInfo = input("param.captchaInfo");
            $keep = input("param.keep");
            $data = [
                'account' =>$user_number,
                'password' =>$user_pass,
                'captchaId' =>$captchaId,
                'captchaInfo' =>$captchaInfo,
            ];
            // 验证字段
            $validate = (new \app\api\validate\UserInfo())->scene('sign_in');
            if(!$validate->check($data)){
                return returnResult(config("config.code.error"),$validate->getError());
            }

            // 验证码处理
            $captchaObj = new \app\common\lib\CaptchaLib\ClickCaptcha();
            if (!$captchaObj->check($data['captchaId'], $data['captchaInfo'])) {
                return returnResult(config("config.code.error"),config("config.describe.captcha.error"));
            }

            // 业务处理
            $userInfo = \app\api\business\LoginBus::login($data);
            $result['userInfo'] = $userInfo;
            if($result){
                return returnResult(config("config.code.success"),config("config.describe.login_successful"),$result);
            }else{
                return returnResult(config("config.code.error"),config("config.describe.login_failed"));
            }
        }catch (\Exception $e){
            exception($e,'Login.signIn');
        }
    }

    public function index()
    {
        $result = ['captcha'=>true];
        if($result){
            return returnResult(config("config.code.success"),config("config.describe.success"),$result);
        }else{
            return returnResult(config("config.code.error"),config("config.describe.login_failed"));
        }
    }
}
